package com.hsnn.medstgmini.pro.project.service.impl;

import java.sql.Blob;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.hsnn.medstgmini.Constants;
import com.hsnn.medstgmini.pro.project.dao.ContractModelDao;
import com.hsnn.medstgmini.pro.project.model.ContractModel;
import com.hsnn.medstgmini.pro.project.model.ContractModelLog;
import com.hsnn.medstgmini.pro.project.service.ContractModelManager;
import com.hsnn.medstgmini.util.Pagination;
import com.hsnn.medstgmini.util.Pdf2SwfUtils;

@Service
public class ContractModelManagerImpl implements ContractModelManager{
	
	@Autowired
	private ContractModelDao contractModelDao;
	
	@Value("#{sysProp['system.swftoolspath']}")
	private String swftoolspath;//swftoolspath路径
	/**
	 * @category 获取合同模板列表页
	 * @author zhouqi
	 * @date 2015年9月8日10:08:08
	 * @return Pagination
	 */
	public Pagination getContractModelListPage(Pagination page){
		return contractModelDao.getContractModelListPage(page);
	}
	
	/**
	 * @category 通过id获取合同模板
	 * @author zhouqi
	 * @date 2015年9月8日10:08:08
	 * @param id
	 * @return Pagination
	 */
	public ContractModel getContractModelById(String id){
		ContractModel contractModel = contractModelDao.getContractModelById(id);
		Blob blob = contractModel.getBlobContent();
		String content = "";
		try {
			if(null!=blob){
				content = new String(blob.getBytes((long)1, (int)blob.length()),"utf-8");
				content = content.replaceAll("\"", "'");//主要为了解决前台多加引号问题
				content = content.replaceAll("\\r","").replaceAll("\\n", "");
				contractModel.setContent(content);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return contractModel;
	}
	
	/**
	 * @category 保存或修改合同模板
	 * @author zhouqi
	 * @date 2015年9月8日10:08:08
	 * @param type 操作类型，修改或添加
	 * @param contractModel
	 * @return Boolean
	 */
	public Boolean saveOrUpdateContractModel(Integer type, ContractModel contractModel){
		return contractModelDao.saveOrUpdateContractModel(type, contractModel);
	}
	
	/**
	 * @category 获取合同模板页日志列表
	 * @author zhouqi
	 * @date 2015年9月8日10:08:08
	 * @return Pagination
	 */
	public Pagination getContractModelLogListPage(Pagination page){
		return contractModelDao.getContractModelLogListPage(page);
	}
	
	/**
	 * @category 保存或修改合同模板日志
	 * @author zhouqi
	 * @date 2015年9月8日10:08:08
	 * @param contractModelLog
	 * @param type 操作类型，修改或添加
	 * @return Boolean
	 */
	public Boolean saveOrUpdateContractModelLog(Integer type, ContractModelLog contractModelLog){
		return contractModelDao.saveOrUpdateContractModelLog(type, contractModelLog);
	}
	
	/**
	 * @category 写日志
	 * @author zhouqi
	 * @date 2015年9月8日19:58:31
	 * @param contractModels
	 */
	@Override
	public void writeLog(ContractModel contractModel) {
		ContractModelLog contractModelLog = new ContractModelLog();
		BeanUtils.copyProperties(contractModel, contractModelLog);
		contractModelLog.setLogId(UUID.randomUUID().toString());
		contractModelDao.saveOrUpdateContractModelLog(Constants.OP_TYPE_ADD, contractModelLog);
	}
	
	/**
	 * @category 获取修改前后对比
	 * @author zhouqi
	 * @date 2015年9月9日08:49:26
	 * @param contractModelId
	 * @param updDateTime
	 * @return List<ContractModelLog>
	 * @throws SQLException 
	 */
	@Override
	public List<ContractModelLog> getCompareContactModelLogList(
			String contractModelId, String updDateTime){
		List<ContractModelLog> contractModelLogList =  contractModelDao.getCompareContactModelLogList(contractModelId, updDateTime);
		try{
			for(int i = 0; i < contractModelLogList.size(); i++){
				ContractModelLog log = contractModelLogList.get(i);
				Blob blob = log.getBlobContent();
				String content = new String(blob.getBytes(1, (int) blob.length()),"utf-8");
				log.setContent(content);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	    return contractModelLogList;
	}
	
	/**
	 * @category 生成PDF版合同
	 * @author zhouqi
	 * @date 2015年9月15日16:28:44
	 * @param content
	 * @param url
	 * @return void
	 */
	@Override
	public void geneContractModelPDF(String fontPath, String content, String pdfurl) {
		try {
			Pdf2SwfUtils.string2PDF(fontPath, content, pdfurl);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
    
	/**
	 * @category 生成SWF版合同
	 * @author zhouqi
	 * @date 2015年9月15日16:28:44
	 * @param pdfToosURL pdfToolsURL的安装路径
	 * @param pdfURL pdf的路径
	 * @param swfURL swfURL的路径
	 * @return void
	 */
	@Override
	public void geneContractModelSWF(String pdfToosURL, String pdfURL, String swfURL) {
		Pdf2SwfUtils.pdf2Swf(pdfToosURL, pdfURL, swfURL);
		
	}
	
	/**
	 * @category 生成pdf和swf
	 * @author zhouqi
	 * @date 2015年9月16日09:25:28
	 * @param contracModel
	 * @param name
	 * @return void
	 */
	@Override
	public ContractModel genePdfAndSwf(ContractModel contractModel, HttpServletRequest request){
		String code = contractModel.getContractModelCode();
		if(StringUtils.isNotBlank(code)){
			String rootPath = request.getSession().getServletContext().getRealPath("/");//获取根路径
			String fontPath = rootPath.replaceAll("\\\\", "/") +"WEB-INF/font/SIMKAI.TTF";//字体路径
			String path = ContractModelManagerImpl.class.getClassLoader().getResource("").getPath();//当前类所在路径
			path = path.replaceAll("\\\\", "/");
			this.geneContractModelPDF(fontPath, contractModel.getContent(), path+"../../pdffile/"+code+".pdf");
			this.geneContractModelSWF(swftoolspath, rootPath+"pdffile/"+code+".pdf", rootPath+"swffile/"+code+".swf");
			contractModel.setPfdFilePath("pdffile/"+code+".pdf");
			contractModel.setSwfFilePath("swffile/"+code+".swf");
		}else{
			code = getContractModelCode();
			String rootPath = request.getSession().getServletContext().getRealPath("/");
			String fontPath = rootPath.replaceAll("\\\\", "/") +"WEB-INF/font/SIMKAI.TTF";//字体路径
			String path = ContractModelManagerImpl.class.getClassLoader().getResource("").getPath();
			path = path.replaceAll("\\\\", "/");
			this.geneContractModelPDF(fontPath,contractModel.getContent(), path+"../../pdffile/"+code+".pdf");
			this.geneContractModelSWF(swftoolspath, rootPath+"pdffile/"+code+".pdf", rootPath+"swffile/"+code+".swf");
			contractModel.setPfdFilePath("pdffile/"+code+".pdf");
			contractModel.setSwfFilePath("swffile/"+code+".swf");
		}
		return contractModel;
	}
	
	/**
	 * @category 获取模板code
	 * @author zhouqi
	 * @param void
	 * @return String 
	 */
	@Override
	public String getContractModelCode(){
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
		String time = format.format(new Date());
		String code = "MB"+time;
		return code;
	}
	/**
	 * @category 格式化文本
	 * @author zhouqi
	 * @param void
	 * @return String 
	 */
	public String formatString(String content){
		
		return null;
	}
}
